package com.healer.springcloud.controller;

import com.healer.springcloud.pojo.Department;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/consumer")
public class DepartmentConsumerController {
//    消费者不应该有service层
//    RestTemplate ，供我们直接调用就可以了！注册到spring中
    @Autowired
    private RestTemplate restTemplate;

//    private final String REST_URL_PREFIX="http://localhost:8001/";
    private final String REST_URL_PREFIX="http://SPRINGCLOUD-PROVIDER-DEPARTMENT/";
    @RequestMapping("/department/querydepartment")
    public List<Department> queryDepartment(){
        return restTemplate.getForObject(REST_URL_PREFIX+"department/querydepartment",List.class);
    }

    @RequestMapping("/department/adddepartment/{name}")
    public boolean adddepartment(@PathVariable("name") String name){
        return restTemplate.getForObject(REST_URL_PREFIX+"department/adddepartment/"+name,Boolean.class);
    }

    @RequestMapping("/department/querydepartmentbyid/{id}")
    public Department queryDepartmentById(@PathVariable("id") Long id){
        Map<String,Object> map=new HashMap<>();
        map.put("id",id);
        System.out.println("querydepartmentbyid到底能不能行啊");
        return restTemplate.postForObject(REST_URL_PREFIX+"department/querydepartmentbyid",map,Department.class);
    }
}
